package com.yc.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yc.R_Code;
import com.yc.Result;
import com.yc.entities.BloodSugarArticleTry;
import com.yc.entities.BloodSugarQualityControl;
import com.yc.service.BloodSugarArticleTryService;
import com.yc.service.BloodSugarPatientInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 试纸批号表 前端控制器
 * </p>
 *
 * @author kuakha
 * @since 2021-04-20
 */
@RestController
@RequestMapping("/bloodSugarArticleTry")
@Api(value = "BloodSugarArticleTryController", tags = {"试纸批号"})
public class BloodSugarArticleTryController {

    @Autowired
    private BloodSugarArticleTryService bloodSugarArticleTryService;


    @ApiOperation(value = "pc添加试纸批次号信息")
    @PreAuthorize("hasAnyAuthority('sys:bedManage','sys:noAccess')")
    @PostMapping("addArticleTry")
    @Transactional(rollbackFor = Exception.class)
    public Result addArticleTryInfo(@Valid @RequestBody BloodSugarArticleTry bloodSugarArticleTry) {
        LambdaQueryWrapper<BloodSugarArticleTry> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(BloodSugarArticleTry::getArticleTry, bloodSugarArticleTry.getArticleTry());
        List<BloodSugarArticleTry> list = bloodSugarArticleTryService.list(wrapper);
        if (list.size() > 0) {
            return Result.error().message("已经存在该试纸批次号");
        }
        bloodSugarArticleTry.setAddTime(LocalDateTime.now());
        boolean save = bloodSugarArticleTryService.save(bloodSugarArticleTry);
        if (save) {
            return Result.ok();
        } else {
            return Result.error().code(R_Code.R_ADD_FAIL);
        }
    }

    @ApiOperation(value = "pc查询所有试纸批次号信息")
    @PreAuthorize("hasAnyAuthority('sys:roleManage','sys:noAccess')")
    @GetMapping(value = {"getArticleTryInfo"})
    public Result getArticleTryInfo(@ApiParam(name = "deanId", value = "院长id") @RequestParam() Integer deanId,
                                    @ApiParam(name = "departmentId", value = "科室id") @RequestParam(required = false) Integer departmentId,
                                    @ApiParam(name = "startTime", value = "起始时间") @RequestParam(required = false) String startTime,
                                    @ApiParam(name = "endTime", value = "结束时间") @RequestParam(required = false) String endTime,
                                    @ApiParam(name = "current", value = "当前页码") @RequestParam(required = false) Integer current,
                                    @ApiParam(name = "size", value = "一页显示多少条") @RequestParam(required = false) Integer size) {
        return bloodSugarArticleTryService.getArticleTryInfo(deanId,departmentId,startTime,endTime,current, size);
    }

    @ApiOperation(value = "pc查询所有试纸批次号信息 下拉框使用")
    @PreAuthorize("hasAnyAuthority('sys:roleManage','sys:noAccess')")
    @GetMapping(value = {"getAllArticleTryInfo"})
    public Result getAllArticleTryInfo(@ApiParam(name = "deanId", value = "院长id") @RequestParam() Integer deanId,
                                    @ApiParam(name = "departmentId", value = "科室id") @RequestParam(required = false) Integer departmentId) {
        LambdaQueryWrapper<BloodSugarArticleTry> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(BloodSugarArticleTry::getDeanId,deanId).eq(BloodSugarArticleTry::getDepartmentId,departmentId);
        List<BloodSugarArticleTry> list = bloodSugarArticleTryService.list(wrapper);
        if (CollectionUtils.isNotEmpty(list)) {
            return Result.ok().data(list);
        } else {
            return Result.error().code(R_Code.R_NULL_DATA_FAIL);
        }
    }

    @ApiOperation(value = "APP通过院长Id和科室Id查看试纸批号")
    @PostMapping("getAllSugarArticleTryByDeanIdAndDepId")
    public Result getAllSugarArticleTryByDeanIdAndDepId(String departmentId, String deanId){
        if (StringUtils.isEmpty(departmentId)) {
            return Result.error().code(301).message("科室Id不能为空");
        }
        if (StringUtils.isEmpty(deanId)) {
            return Result.error().code(302).message("院长Id不能为空");
        }

        QueryWrapper<BloodSugarArticleTry> query = Wrappers.query();
        query.eq("department_id", departmentId);
        query.eq("dean_id", deanId);
        List<BloodSugarArticleTry> list = bloodSugarArticleTryService.list(query);
        if (CollectionUtils.isNotEmpty(list)) {
            return Result.ok().data(list);
        } else {
            return Result.error().code(300).message("暂无数据");
        }
    }


}

